#include "tcp_client.h"

TCP_client::TCP_client(QWidget *parent) : QWidget(parent)
{
    // 检测连接成功
    connect( &this->socket , &QTcpSocket::connected , this , [&](){
        // 向界面发送连接成功
        emit connect_sig();
    });
    // 检测连接断开
    connect( &this->socket , &QTcpSocket::disconnected , this , [&](){
        // 向界面发送连接断开
        emit dis_connect_sig();
    });
    // 检测服务器数据写入
    connect( &this->socket , &QTcpSocket::readyRead , this , [&](){
        // 从套接字读取数据
        QString msg = this->socket.readAll();
        // 将数据发送给主机
        emit read_sig(msg);
    });
}



void TCP_client::set_connect(const QString &address, quint16 port)
{
    this->HostAddr = address;
    this->port = port;
}

void TCP_client::start_connect()
{
    // 启动连接
    this->socket.connectToHost( this->HostAddr , this->port );

}

void TCP_client::send_msg(QString msg)
{
    QByteArray a;
    a.clear();
    a += msg;
    this->socket.write(a);
}
